package com.hamizhong.iflowers.db.dao.hibernate.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.hamizhong.iflowers.db.dao.base.MyBaseHibernateDao;
import com.hamizhong.iflowers.db.dao.hibernate.IOrdersDao;
import com.hamizhong.iflowers.db.entity.Orders;
import com.hamizhong.iflowers.utils.MyColumnIndicator;

public class OrdersDaoImpl extends MyBaseHibernateDao<Orders, Integer> implements IOrdersDao {

	public List<Orders> findByStateAndUserId(final Integer orderState, final Integer userId, final Integer pageNum, final Integer pageSize) {
		List list = null;
		try {
			List result = this.getHibernateTemplate().executeFind(new HibernateCallback() {
				public Object doInHibernate(Session session) throws HibernateException, SQLException {
					Query query = null;
					if(orderState == MyColumnIndicator.O_ST_ALL) {
						String hql = "from Orders t ";
						hql += " where t.user = :u ";
						hql += " order by t.createDate desc";
						query = session.createQuery(hql);
						query.setInteger("u", userId);
						query.setFirstResult((pageNum-1)*pageSize);
						query.setMaxResults(pageSize);
					}else {
						String hql = "from Orders t ";
						hql += " where t.user = :u and t.orderState = :os ";
						hql += " order by t.createDate desc";
						query = session.createQuery(hql);
						query.setInteger("os", orderState);
						query.setInteger("u", userId);
						query.setFirstResult((pageNum-1)*pageSize);
						query.setMaxResults(pageSize);
					}
					return query.list();
				}
			});
			list = result;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return list;
	}

	public List<Orders> findByState(final Integer orderState, final Integer pageNum, final Integer pageSize) {
		List list = null;
		try {
			List result = this.getHibernateTemplate().executeFind(new HibernateCallback() {
				public Object doInHibernate(Session session) throws HibernateException, SQLException {
					Query query = null;
					if(orderState == MyColumnIndicator.O_ST_ALL) {
						String hql = "from Orders t ";
						hql += " order by t.createDate desc";
						query = session.createQuery(hql);
						query.setFirstResult((pageNum-1)*pageSize);
						query.setMaxResults(pageSize);
					}else {
						String hql = "from Orders t ";
						hql += " where t.orderState = :os ";
						hql += " order by t.createDate desc";
						query = session.createQuery(hql);
						query.setInteger("os", orderState);
						query.setFirstResult((pageNum-1)*pageSize);
						query.setMaxResults(pageSize);
					}
					return query.list();
				}
			});
			list = result;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return list;
	}

	public int findTotalOrdersCntByOrderState(final Integer orderState) {
		try {
			List totalCntList = this.getHibernateTemplate().executeFind(new HibernateCallback() {
				public Object doInHibernate(Session session)
						throws HibernateException, SQLException {
					String hql = "";
					Query query = null;
					if(orderState == MyColumnIndicator.O_ST_ALL) {
						hql += "select count(*) from Orders t ";
						query = session.createQuery(hql);
					}else {
						hql += "select count(*) from Orders t ";
						hql += "where t.orderState = :os ";
						query = session.createQuery(hql);
						query.setInteger("os", orderState);
					}
					return query.list();
				}
			});
			return Integer.valueOf(totalCntList.get(0).toString()).intValue();
		} catch (Exception e) {
			e.printStackTrace();
			return 0;
		}
	}

	public int findTotalOrdersCntByOrderStateAndUserId(final Integer orderState, final Integer userId) {
		try {
			List totalCntList = this.getHibernateTemplate().executeFind(new HibernateCallback() {
				public Object doInHibernate(Session session)
						throws HibernateException, SQLException {
					String hql = "";
					Query query = null;
					if(orderState == MyColumnIndicator.O_ST_ALL) {
						hql += "select count(*) from Orders t ";
						hql += " where t.user = :u ";
						query = session.createQuery(hql);
						query.setInteger("u", userId);
					}else {
						hql += "select count(*) from Orders t ";
						hql += "where t.user = :u ";
						hql += "and t.orderState = :os ";
						query = session.createQuery(hql);
						query.setInteger("u", userId);
						query.setInteger("os", orderState);
					}
					return query.list();
				}
			});
			return Integer.valueOf(totalCntList.get(0).toString()).intValue();
		} catch (Exception e) {
			e.printStackTrace();
			return 0;
		}
	}


}
